package com.itcast.n9_垃圾回收;

import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;

/**
 * @program: jvm
 * @description: -Xmx200m：最多能放一个100m的数组
 * @author: flyduck
 * @create: 2024-09-30 10:16
 **/
public class Test09_常见的引用对象_软引用里面数据回收后_通过队列方式回收软引用本身对象 {
    public static void main(String[] args) throws IOException {
        ArrayList<SoftReference> softReferences = new ArrayList<>();//这是是防止软引用的引用被回收了
        ReferenceQueue<byte[]> queues = new ReferenceQueue<byte[]>();//里面放的是被回收的软引用
        for (int i = 0; i < 10; i++) {
            byte[] bytes = new byte[1024 * 1024 * 100];
            SoftReference studentRef = new SoftReference<byte[]>(bytes,queues);
            softReferences.add(studentRef);
        }

        SoftReference<byte[]> ref = null;
        int count = 0;
        while ((ref = (SoftReference<byte[]>) queues.poll()) != null) {
            count++;//9：里面放的是被回收的软引用
        }
        System.out.println(count);

    }
}
